package sk.stuba.fiit.ltasys.web.mvc.model.entity;

import java.util.Date;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import org.hibernate.annotations.LazyToOne;
import org.hibernate.annotations.LazyToOneOption;

/**
 * EvidenceRecord Entity.
 * 
 * @author Marek Hruska
 */

@Entity
@Table(name = "EVIDENCE_RECORD")
public class EvidenceRecordEntity implements Comparable<EvidenceRecordEntity>{
	
	public static final transient String RECORD_ID = "record_id";

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private int record_id;

	@ManyToMany()
	@LazyCollection(LazyCollectionOption.FALSE)
//	@Cascade({CascadeType.SAVE_UPDATE})
	private Set<CrlEntity> crls;
	
	@ManyToMany()
	@LazyCollection(LazyCollectionOption.FALSE)
//	@Cascade({CascadeType.SAVE_UPDATE})
	private Set<CertificateEntity> certificates;
	
	@OneToOne()
	@LazyToOne(LazyToOneOption.FALSE)
	@Cascade({CascadeType.ALL})
	private TimestampEntity timestamp;
	
	private Date created;
	
	public EvidenceRecordEntity() {}

	public int getRecord_id() {
		return record_id;
	}

	public void setRecord_id(int record_id) {
		this.record_id = record_id;
	}

	public Set<CrlEntity> getCrls() {
		return crls;
	}

	public void setCrls(Set<CrlEntity> crls) {
		this.crls = crls;
	}

	public Set<CertificateEntity> getCertificates() {
		return certificates;
	}

	public void setCertificates(Set<CertificateEntity> certificates) {
		this.certificates = certificates;
	}

	public TimestampEntity getTimestamp() {
		return timestamp;
	}

	public void setTimestamp(TimestampEntity timestamp) {
		this.timestamp = timestamp;
	}

	public int compareTo(EvidenceRecordEntity o) {
		if( this.created != null && o.created !=null && this.created.after(o.created))
			return 1;
		else{
			return -1;
		}
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((certificates == null) ? 0 : certificates.hashCode());
		result = prime * result + ((crls == null) ? 0 : crls.hashCode());
		result = prime * result + record_id;
		result = prime * result
				+ ((timestamp == null) ? 0 : timestamp.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		EvidenceRecordEntity other = (EvidenceRecordEntity) obj;
		if (certificates == null) {
			if (other.certificates != null)
				return false;
		} else if (!certificates.equals(other.certificates))
			return false;
		if (crls == null) {
			if (other.crls != null)
				return false;
		} else if (!crls.equals(other.crls))
			return false;
		if (record_id != other.record_id)
			return false;
		if (timestamp == null) {
			if (other.timestamp != null)
				return false;
		} else if (!timestamp.equals(other.timestamp))
			return false;
		return true;
	}

	public Date getCreated() {
		return created;
	}

	public void setCreated(Date created) {
		this.created = created;
	}
	
}
